/**
 * 
 */
package com.simtek.simplayer;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.URLUtil;

import com.simtek.simplayer.controller.PlayerController;
import com.simtek.simplayer.entry.VideoPlayEntry;
import com.simtek.simplayer.entry.VideoTypeConstrant;
import com.simtek.simplayer.utils.FilenameUtils;
import com.simtek.simplayer.utils.StringUtils;
import com.simtek.simplayer.utils.VideoUtils;
import com.simtek.simplayer.window.StandOutWindow;
import com.simtek.simplayer.window.ui.Window;

/**
 * @author Hitech
 * 
 */
public class VideoOpenHandlerActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		handleAction(getIntent());

		finish();
	}

	private boolean handleAction(Intent intent) {
		if (intent != null) {
			String action = intent.getAction();
			if (action != null) {
				VideoPlayEntry playEntry = new VideoPlayEntry();
				if (Intent.ACTION_VIEW.equals(action)) {
					Uri data = intent.getData();

					if (URLUtil.isContentUrl(data.toString())) {
						playEntry.setSrcType(VideoTypeConstrant.TYPE_VIDEO_LOCAL);
						playEntry.setUri(data);
						String[] info = StringUtils.getVideoInfoByUri(this, data);
						if (info != null) {
							playEntry.setVideoSrc(info[0]);
							playEntry.setTitle(info[1]);
						}
					} else if (URLUtil.isFileUrl(data.toString())) {
						playEntry.setSrcType(VideoTypeConstrant.TYPE_VIDEO_LOCAL);
						playEntry.setVideoSrc(data.getPath());
						playEntry.setTitle(FilenameUtils.getBaseName(data
								.getPath()));
						playEntry.setUri(data);
					} else {
						String url = data.toString();
						if (URLUtil.isHttpsUrl(url) || URLUtil.isHttpUrl(url)) {
							String youtubeCode = VideoUtils.getYoutubeCode(url);
							Log.d("simfetch", url + " => " + youtubeCode);
							if (StringUtils.isNotBlank(youtubeCode)) {
								playEntry.setSrcType(VideoTypeConstrant.TYPE_VIDEO_YOUTUBE);
								playEntry.setVideoSrc(youtubeCode);
							} else {
								playEntry.setSrcType(VideoTypeConstrant.TYPE_VIDEO_HTTP);
								playEntry.setVideoSrc(data.getPath());
								playEntry.setUri(data);

								playEntry.setTitle(intent.getExtras().getString(Intent.EXTRA_TITLE));
								playEntry.setUid(intent.getExtras().getString(Intent.EXTRA_UID));
							}
						}
					}
				} else if (Intent.ACTION_SEND.equals(action)) {
					if (intent.hasExtra(Intent.EXTRA_STREAM)) {
						Log.d("down", "extra stream ");
						Uri data = intent
								.getParcelableExtra(Intent.EXTRA_STREAM);
						if (URLUtil.isContentUrl(data.toString())) {
							playEntry
									.setSrcType(VideoTypeConstrant.TYPE_VIDEO_LOCAL);
							playEntry.setVideoSrc(StringUtils
									.getVideoPathByUri(this, data));
						} else if (URLUtil.isFileUrl(data.toString())) {
							playEntry
									.setSrcType(VideoTypeConstrant.TYPE_VIDEO_LOCAL);
							playEntry.setVideoSrc(data.getPath());
						} else {
							String url = data.toString();
							if (URLUtil.isHttpsUrl(url)
									|| URLUtil.isHttpUrl(url)) {

								String youtubeCode = VideoUtils.getYoutubeCode(url);
								if (!StringUtils.isBlank(youtubeCode)) {
									playEntry.setSrcType(VideoTypeConstrant.TYPE_VIDEO_YOUTUBE);
									playEntry.setVideoSrc(youtubeCode);
								} else {
									playEntry.setSrcType(VideoTypeConstrant.TYPE_VIDEO_HTTP);
									playEntry.setVideoSrc(data.getPath());
								}
							}
						}
					} else if (intent.hasExtra(Intent.EXTRA_TEXT)) {
						String text = intent.getStringExtra(Intent.EXTRA_TEXT);
						List<String> httpLink = StringUtils.getHttpLink(text);
						if (httpLink == null || httpLink.isEmpty()) {
							return false;
						}

						String path = httpLink.get(0);
						String youtubeCode = VideoUtils.getYoutubeCode(path);
						if (!StringUtils.isBlank(youtubeCode)) {
							playEntry.setSrcType(VideoTypeConstrant.TYPE_VIDEO_YOUTUBE);
							playEntry.setVideoSrc(youtubeCode);
						} else {
							// TODO What with other link
							Log.d("sim", path);
							return false;
						}
					}
				}

				PlayerController instance = PlayerController.getInstance();
				if (!instance.isPlayingVideoEntry(playEntry)) {
					instance.setVideoSrc(playEntry);
					Window window = StandOutWindow.findWindowById(VideoPlayerWindow.getWindowId(), VideoPlayerWindow.class);
					if (window == null) {
						// show player window
						StandOutWindow.closeAll(this, VideoPlayerWindow.class);
						StandOutWindow.show(this, VideoPlayerWindow.class, StandOutWindow.DEFAULT_ID);
					} else {
						instance.onCreate(this);
					}
					return true;
				}
			}
		}
		return false;
	}
}
